using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace SSDS
{
    public class MainForm : System.Windows.Forms.Form
    {
        private int                               m_MaxDisplayLines;    // Max lines onscreen at once
        private TextFile                          m_CurrentFile;        // Current file we are viewing
        private FileHistory                       m_History;            // History of files searched
        private Prompt                            m_Prompt;	            // Current user entry dialog
        private System.Windows.Forms.RichTextBox  m_Text;               // Current on-screen text

        private System.ComponentModel.Container   components = null;


        public MainForm()
        {
            InitializeComponent();

            // Load or create the file history
            m_History = new FileHistory(this);

            m_MaxDisplayLines = 35;
        }

        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.m_Text = new System.Windows.Forms.RichTextBox();
            this.SuspendLayout();
            // 
            // m_Text
            // 
            this.m_Text.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.m_Text.BackColor = System.Drawing.Color.Teal;
            this.m_Text.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.m_Text.ForeColor = System.Drawing.SystemColors.Info;
            this.m_Text.Location = new System.Drawing.Point(0, 0);
            this.m_Text.Name = "m_Text";
            this.m_Text.ReadOnly = true;
            this.m_Text.Size = new System.Drawing.Size(288, 264);
            this.m_Text.TabIndex = 0;
            this.m_Text.Text = "";
            // 
            // MainForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.BackColor = System.Drawing.SystemColors.Control;
            this.ClientSize = new System.Drawing.Size(284, 264);
            this.Controls.Add(this.m_Text);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.Name = "MainForm";
            this.Text = "Spectate Swamp Desktop Search (stonedan@telusplanet.net)";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Load += new System.EventHandler(this.OnLoad);
            this.ResumeLayout(false);

        }
        #endregion


        [STAThread]
        static void Main() 
        {
            Application.Run(new MainForm());
        }

        private void DisplayFile()
        {
            if (m_CurrentFile == null) 
            {
                return;
            }

            m_Text.Text		 = "";           // Clear the display
            bool need_prompt = false;
            bool keep_going  = false;
            for (int line_number = 0; line_number < m_CurrentFile.Lines.Count; line_number++)
            {
                if (!keep_going && 
                    need_prompt &&
                    line_number % m_MaxDisplayLines == 0)
                {
                    m_Prompt            = new Prompt();
                    m_Prompt.Text       = string.Format("Prompt #4  {0}", m_CurrentFile.Name);
                    m_Prompt.Label.Text = "Enter for next SCREEN y/n <y> 'a' =all 'b' =back";
                    m_Prompt.Entry.Text = "Y";
                    m_Prompt.ShowDialog(this);

                    m_Text.Text = "";
                    
                    if (m_Prompt.DialogResult == DialogResult.OK)
                    {
                        string Option = m_Prompt.Entry.Text.ToUpper();
                        if (Option == "A")
                        {
                            keep_going = false;
                        }
                        else if (Option == "B")
                        {
                            line_number -= (m_MaxDisplayLines * 2 + 1);
                            need_prompt = false;
                            if (line_number < -1)
                            {
                                return;
                            }
                            continue;
                        }
                        m_Text.Text += (string)m_CurrentFile.Lines[line_number] + System.Environment.NewLine;
                        continue;
                    }
                    return;
                }
                else
                {
                    m_Text.Text += (string)m_CurrentFile.Lines[line_number] + System.Environment.NewLine;
                    need_prompt = true;
                }
            }
            
            m_Prompt            = new Prompt();
            m_Prompt.Text       = string.Format("EOF  {0} {1} LINES", m_CurrentFile.Name, m_CurrentFile.Lines.Count);
            m_Prompt.Label.Text = "Enter for next SCREEN y/n <y> 'a' =all 'b' =back";
            m_Prompt.Entry.Text = "Y";
            m_Prompt.ShowDialog(this);
        }

        private void ShowPrompt2(string Filename)
        {
            m_Text.Text			= "";           // Clear the display

            // Show the options prompt
            m_Prompt            = new Prompt();
            m_Prompt.Text       = string.Format("Option Prompt #1  {0}", Filename);
            m_Prompt.Label.Text = "P P1 P2 TT WW SS CH X RAND NORAND RANDA\r\nNORANDA THUMB HELP option";
            m_Prompt.Entry.Text = "Y";

            m_Prompt.ShowDialog(this);

            if (m_Prompt.DialogResult == DialogResult.OK)
            {
                if (m_Prompt.Entry.Text.ToUpper() == "HELP")
                {
                    m_CurrentFile = new HelpFile(this);
                    DisplayFile();
                }
                else if (m_Prompt.Entry.Text.ToUpper() == "Y")
                {
                    DisplayFile();
                }
                // More to come...
            }
        }

        private void ShowPrompt1()
        {
            m_Text.Text = "";

            int index   = 1;
            foreach (string filename in m_History.Lines)
            {
                m_Text.Text += string.Format("{0}\t\t{1}{2}", index, filename, System.Environment.NewLine);
                index++;
            }

            m_Prompt            = new Prompt();
            m_Prompt.Text       = string.Format("File Prompt #1  {0}", DateTime.Now.ToString());
            m_Prompt.Label.Text = "File Selection:";
            m_Prompt.Entry.Text = (string)m_History.Lines[0];
            m_Prompt.ShowDialog(this);

            if (m_Prompt.DialogResult == DialogResult.OK)
            {
                string Filename;
                int    HistoryIndex = -1;
                try 
                {
                    HistoryIndex = int.Parse(m_Prompt.Entry.Text) - 1;
                }
                catch (Exception)
                {}

                if (HistoryIndex >= 0 && HistoryIndex < FileHistory.MAX_HISTORY)
                {
                    Filename = (string)m_History.Lines[HistoryIndex];
                }
                else
                {
                    Filename = m_Prompt.Entry.Text;
                }

                if (Filename.Length < 4 || 
                        Filename.Substring(Filename.Length - 4, 4).ToUpper() != ".TXT")
                {
                    return;
                }

                if (Filename.ToUpper() == "HELP.TXT")
                {
                    m_CurrentFile = new HelpFile(this);
                }
                else
                {
                    m_CurrentFile = new TextFile(this, Filename, true);
                }

                m_History.Add(Filename);
                ShowPrompt2(Filename);
            }
            else
            {
                Close();
            }
        }

        private void OnLoad(object sender, System.EventArgs e)
        {
            WindowState = FormWindowState.Maximized;

            // Keep showing the prompt until this dialog gets closed somewhere
            while (!IsDisposed)
            {
                ShowPrompt1();
            }

            // Save the history again
            m_History.WriteFile();
        }
    }
}
